// ...........................................................................................................
// (C) Copyright  1996/2008 Fuego Inc.  All Rights Reserved
// THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Fuego Inc.
// The copyright notice above does not evidence any actual or intended
// publication of such source code.
//
// Last changed on 2008-01-11 17:04:46 (-0300), by: machaval. $Revision$
// ...........................................................................................................

package com.webcamtracker.ui;

import com.webcamtracker.video.VideoSuscriptor;

import javax.media.format.VideoFormat;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

public class UIDrawableCanvas
        extends Canvas
        implements VideoSuscriptor, VideoListener {
    //~ Instance fields ......................................................................................

    private List<Drawable> drawables;
    private Dimension size;
    private BufferedImage image;

    //~ Constructors .........................................................................................

    public UIDrawableCanvas(Dimension size) {
        super();
        this.size = size;
        drawables = new ArrayList<Drawable>();
        setSize(size);
    }

    //~ Methods ..............................................................................................

    public void arrivedFrame(BufferedImage image) {
        this.image = image;
        repaint();
    }

    public boolean isDoubleBuffered() {
        return true;
    }

    public boolean add(Drawable o) {
        return drawables.add(o);
    }

    public boolean remove(Drawable o) {
        return drawables.remove(o);
    }

    public void clear() {
        drawables.clear();
        image = null;
        repaint();
    }

    public Dimension getPreferredSize() {
        return size;
    }

    public void paint(Graphics g) {
        if (image != null) {
            g.drawImage(image, 0, 0, null);
        }
        for (Drawable drawable : drawables) {
            drawable.paint(g);
        }
    }

    public void update(Graphics g) {
        paint(g);
    }

    public BufferedImage getImage() {
        return image;
    }

    public void videoStarted(VideoFormat format) {
        size = format.getSize();
        repaint();
    }

    public void videoStopped() {
        clear();
    }

    //~ Inner Interfaces .....................................................................................

    public static interface Drawable {
        void paint(Graphics g);
    }
}
